package com.chinacoal.hr.levelimpl.controller;

import com.chinacoal.hr.levelapi.client.ApprovalClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelapi.entity.AttendanceAppeal;
import com.chinacoal.hr.levelapi.entity.BizTravel;
import com.chinacoal.hr.levelapi.entity.LeaveRecord;
import com.chinacoal.hr.levelimpl.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-27 18:06
 * @description: 专门提供审批Controller
 **/
@RestController
public class ApprovalController implements ApprovalClient{

    @Autowired
    private ApprovalService approvalService;

    /**
     * 根据流程实例ID查询待审批的出差申请单
     *
     * @param insCode : 流程实例ID
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */

    public Result travelInfo(String insCode) {

        BizTravel travel = null;
        try {
            travel = approvalService.getTravelInfoForApproval(insCode);
        } catch (Throwable e) {
            ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(travel);
    }

    /**
     * 审批通过出差申请单
     *
     * @param travelId :
     * @param record   :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    public Result travelApprove( String travelId, BizTravel record){
        try {
            Map<String, String> result = approvalService.travelApproval(travelId, record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 驳回出差申请单
     *
     * @param travelId :
     * @param record   :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result travelApproveDisagree( String travelId, BizTravel record) {
        try {
            Map<String, String> result = approvalService.travelApprovalDisagree(travelId, record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 根据考勤申诉表编码查询待审批的考勤申诉信息
     *
     * @param insCode : 流程实例ID
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */

    public Result appealInfo(String insCode) {

        AttendanceAppeal attendanceAppeal = null;
        try {
            attendanceAppeal = approvalService.getAppealForApproval(insCode);
        } catch (Throwable e) {
            ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(attendanceAppeal);
    }

    /**
     * 审批通过考勤申诉单
     *
     * @param explainId        :
     * @param attendanceAppeal :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    public Result appealApprove(@PathVariable String explainId,
                                @RequestBody AttendanceAppeal attendanceAppeal) {
        try {
            Map<String, String> result = approvalService.appealApproval(explainId, attendanceAppeal);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 驳回考勤申诉单
     *
     * @param explainId        :
     * @param attendanceAppeal :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */

    public Result appealApproveDisagree( String explainId, AttendanceAppeal attendanceAppeal) {
        try {
            Map<String, String> result = approvalService.appealApprovalDisagree(explainId, attendanceAppeal);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }


    /**
     * 获得请假单基本信息
     *
     * @param insCode : 流程实例编码
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/28 2019/11/28
     */

    public Result leaveInfo(String insCode) {

        LeaveRecord leaveRecord = null;
        try {
            leaveRecord = approvalService.getLeaveInfoFotApproval(insCode);
        } catch (Throwable e) {
            ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(leaveRecord);
    }

    /**
     * 审批通过请假单
     *
     * @param leaveId :
     * @param record  :
     * @return : com.chinacoal.hr.levelapi.response.Result
     * @author : YunTao.Li
     * @date : 2019/11/28 2019/11/28
     */
    public Result leaveApprove( String leaveId, LeaveRecord record) {
        try {
            Map<String, String> result = approvalService.leaveApproval(leaveId, record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 驳回请假单
     *
     * @param leaveId :
     * @param record  :
     * @return : com.chinacoal.hr.levelimpl.response.Result
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public Result leaveApproveDisagree( String leaveId, LeaveRecord record) {
        try {
            Map<String, String> result = approvalService.leaveApprovalDisagree(leaveId, record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return ResultGenerator.genOkResult();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /***
     * 参数: processInstID , data
     *   查询审批历史
     * @return  com.chinacoal.hr.levelimpl.response.Result
     * 创建人: jinyabin
     * 创建时间: 2020/10/29 14:12
     */

    public Result historys( String processInstID, Object data) {

        // 构建查询条件
        List recordList = null;
        try {
            recordList = this.approvalService.getApprovalHistorys(processInstID);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return ResultGenerator.genOkResult(recordList);
    }

}
